<template>
  <div>
    名字：
    <el-input v-model="studentName" style="width: 60px"></el-input>
    性别：
    <el-radio-group v-model="sex">
      <el-radio label="1" size="large">男</el-radio>
      <el-radio label="2" size="large">女</el-radio>
    </el-radio-group>

    开始时间：
    <el-date-picker v-model="startTime" type="date" placeholder="选择日期" value-format="YYYY--MM--DD"/>
    结束时间：
    <el-date-picker v-model="endTime" type="date" placeholder="选择日期" value-format="YYYY--MM--DD"/>
    <el-button type="primary" @click="select()">
      <el-icon class="el-icon--left">
        <Search/>
      </el-icon>
    </el-button>
  </div>
  <div>
    <el-table :data="studentList" stripe>
      <!--      prop 表示绑定哪一个属性-->
      <!--      label标头的名称-->
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="sex" label="性别">
        <template #default="scope">
          <!--          三元表达式  三目运算符-->
          {{ scope.row.sex == 1 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="phoneNumber" label="电话"></el-table-column>
      <el-table-column prop="birthday" label="生日"></el-table-column>
      <el-table-column label="操作">
        <!--        scope 得到表单某一行整个对象（包括数据对象，以及页面标签对象等）-->
        <!--        scope.row 得到当前这一行对象-->
        <!--        #default 插槽表达式  scope变量名-->
        <template #default="scope">
          <el-button type="primary" @click="deleteStudent(scope.row.id)">删除</el-button>
          <el-button type="primary" @click="updateStudent(scope.row)">修改</el-button>
          <el-dialog v-model="Visible" title="修改用户" width="30%">
            <!--      <span>这是一个添加用户操作</span>-->
            <el-form :model="studentForm" ref="studentForm">
              <!--<el-form-item 表单中的每一行，由框架底层提供样式布局>-->
              <!--label 表单的描述-->

              <el-form-item label="姓名" prop="studentName">
                <el-input v-model="studentForm.studentName"></el-input>
              </el-form-item>
              <el-form-item label="性别" prop="sex">
                <el-radio-group v-model="studentForm.sex">
                  <el-radio :label="1">男</el-radio>
                  <el-radio :label="2">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="生日" prop="birthday">
                <el-date-picker type="data" value-format="YYYY-MM-DD"
                                v-model="studentForm.birthday" placeholder="请选择生日"></el-date-picker>
              </el-form-item>

              <el-form-item label="年龄" prop="age">
                <el-input v-model="studentForm.age"></el-input>
              </el-form-item>

              <el-form-item label="电话号码" prop="phoneNumber">
                <el-input v-model="studentForm.phoneNumber"></el-input>
              </el-form-item>
            </el-form>
            <!--#footer 表示当前template中内容 当前对话框的脚步      -->
            <template #footer>
              <!--     class="dialog-footer"  使用elmentui中对话框的样式   -->
              <span class="dialog-footer">
                  <el-button type="primary" @click="cancel()">取消</el-button>
                  <el-button type="primary" @click="updateStudentBean(scope.row)">确定 </el-button>
                   </span>
            </template>
          </el-dialog>
        </template>
      </el-table-column>

    </el-table>


    <el-dialog v-model="dialogVisible" title="添加用户" width="30%">
      <!--      <span>这是一个添加用户操作</span>-->
      <el-form :model="studentBeanForm" ref="studentBeanForm">
        <!--<el-form-item 表单中的每一行，由框架底层提供样式布局>-->
        <!--label 表单的描述-->
        <el-form-item label="姓名" prop="name">
          <el-input v-model="studentBeanForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="studentBeanForm.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker type="data" value-format="YYYY-MM-DD"
                          v-model="studentBeanForm.birthday" placeholder="请选择生日"></el-date-picker>
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-input v-model="studentBeanForm.age"></el-input>
        </el-form-item>

        <el-form-item label="电话号码" prop="tel">
          <el-input v-model="studentBeanForm.phoneNumber"></el-input>
        </el-form-item>
      </el-form>
      <!--#footer 表示当前template中内容 当前对话框的脚步      -->
      <template #footer>
        <!--     class="dialog-footer"  使用elmentui中对话框的样式   -->
        <span class="dialog-footer">
        <el-button type="primary" @click="cancel()">取消</el-button>
        <el-button type="primary" @click="addStudent()">确定</el-button>
          </span>
      </template>
    </el-dialog>
    <el-button type="primary" @click="dialogVisible=true">添加用户</el-button>


    <el-pagination v-model:current-page="curPage" @current-change="handleCurrentChange" v-model:page-size="pageSize"
                   background :total="total">

    </el-pagination>
  </div>
</template>

<script>
import axios from "axios";
import {ElMessage} from "element-plus";

export default {
  name: "Student.vue",
  data() {
    return {

      studentList: [],
      curPage: 1,
      pageSize: 5,
      total: 0,
      studentName: '',
      sex: '',
      startTime: '',
      endTime: '',
      dialogVisible: false,
      Visible:false,
      studentForm: {
        studentName: '',
        sex: '',
        birthday: '',
        age: '',
        phoneNumber: ''

      },
      studentBeanForm: {
        name: '',
        sex: '',
        birthday: '',
        age: '',
        phoneNumber: ''

      }
    }


  },
  created() {
    this.getStudent()
  },
  methods: {
    handleCurrentChange(page) {

      this.curPage = page;
      this.getStudent();

    },
    select() {
      this.getStudent();
    },
    updateStudentBean() {

      this.Visible = false
      let param = {
        id: this.id,
        name: this.studentForm.studentName,
        sex: this.studentForm.sex,
        age: this.studentForm.age,
        birthday: this.studentForm.birthday,
        phoneNumber: this.studentForm.phoneNumber
      }

      let url = "/api/student/update";
      axios.post(url, param).then(res => {
        this.getStudent();
        ElMessage({
          message: '修改成功',
          type: 'success'
        })
      })


    },
    addStudent() {

      axios.post("/api/student/install", this.studentBeanForm).then(res => {
        this.getStudent();

        ElMessage({
          message: '添加成功',
          type: 'success'
        })

      })
      this.dialogVisible = false
      this.studentBeanForm = {}

    },
    updateStudent(student) {

      this.Visible=true,
      this.studentForm.studentName = student.name
      this.studentForm.sex = student.sex
      this.studentForm.age = student.age
      this.studentForm.birthday = student.birthday
      this.studentForm.phoneNumber = student.phoneNumber
      this.id = student.id

    },
    cancel() {
      this.Visible=false
      this.dialogVisible = false

      this.studentForm = {}
    },

    deleteStudent(id) {

      let param = new URLSearchParams();
      param.append("id", id);
      let url = "/api/student/delete";
      axios.post(url, param).then(res => {
        this.getStudent();
        ElMessage({
          message: '删除成功',
          type: 'success'
        })
      })
    },
    getStudent() {
      let param = new URLSearchParams();
      param.append("pageNo", this.curPage);
      param.append("pageSize", this.pageSize);
      param.append("name", this.studentName);
      if ('1' == this.sex || '2' == this.sex) {
        param.append("sex", this.sex);
      } else {
        param.append("sex", 0);
      }

      param.append("startTime", this.startTime);
      param.append("endTime", this.endTime);

      let url = "/api/student/list";
      this.student(url, param)
    },
    student(url, param) {
      axios.post(url, param).then(res => {


        this.studentList = res.data.records;
        this.total = res.data.total
        this.studentName=''
        this.sex=''
        this.startTime=''
        this.endTime=''
      })
    }
  }
}
</script>

<style scoped>

</style>